
package com.iweb.filter;


import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@WebFilter(value = "/*")
public class LoginFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("过滤开始");
    }
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        System.out.println("过滤");
        List<String> oks = Arrays.asList("/start","/user/login","/user","/admin","/place/selectAll","/user/addUser"
        ,"/user/selectPhone","/static/js/judge.js");
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse resp = (HttpServletResponse) response;
        String uri = req.getRequestURI();
        System.out.println(uri);
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        if(oks.contains(uri)){
            chain.doFilter(request,response);
        }else{
            Object userID = req.getSession().getAttribute("userID");
            if(userID ==null){
                resp.sendRedirect(req.getContextPath()+"/start");
            }else{
                System.out.println(userID);
                chain.doFilter(request,response);
            }
        }
    }
    @Override
    public void destroy() {

    }
}

